package com.example.imiaow.yyads_text.banner;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.widget.FrameLayout;

import com.example.imiaow.yyads_text.Config;
import com.youyi.yesdk.ad.BannerAd;
import com.youyi.yesdk.business.AdPlacement;
import com.youyi.yesdk.listener.BannerAdListener;

import org.jetbrains.annotations.Nullable;

import java.util.Map;

import io.flutter.Log;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.platform.PlatformView;

public class Banner implements PlatformView {
    private BannerAd mbannerAd;
    private FrameLayout mframeLayout;
    private final Context mcontext;
    private final Activity mactivity;
    private MethodChannel channel;
    private final BinaryMessenger mbinaryMessenger;
    private final int mid;
    private final Map<String, Object> mparams;
    private Float width;
    private Float height;
    Banner(Context context,Activity activity,BinaryMessenger binaryMessenger,int id,Map<String, Object> params){
        this.mcontext = context;
        this.mactivity = activity;
        this.mbinaryMessenger = binaryMessenger;
        this.mid = id;
        this.mparams = params;
    }

    public void init(){
        Config config = new Config();
        width = Float.valueOf(String.valueOf(mparams.get("width")));
        height = Float.valueOf(String.valueOf(mparams.get("height")));
        channel = new MethodChannel(mbinaryMessenger,config.bannerPage+"_"+mid);
        mbannerAd = new BannerAd();
        mframeLayout = new FrameLayout(mcontext);
        mbannerAd.setBannerConfig(mactivity, new AdPlacement
                .Builder()
                .setAdId("0000000502")
                .setExpressViewAcceptedSize(width,height)
                .isCarousel(false)
                .build()
        );
        mbannerAd.loadAdBanner(new BannerAdListener() {
            @Override
            public void onError(@Nullable Integer integer, @Nullable String s) {
                channel.invokeMethod("onError","android_开屏广告展示成功");
            }

            @Override
            public void onLoaded(@Nullable View view) {
                if(view != null){
                    mframeLayout.removeAllViews();
                    mframeLayout.addView(view);
                }
                channel.invokeMethod("onLoaded","android_开屏广告展示成功");
            }

            @Override
            public void onShow() {
                channel.invokeMethod("onShow","android_开屏广告展示成功");
            }

            @Override
            public void onClosed() {
                channel.invokeMethod("onClosed","android_开屏广告展示成功");
            }

            @Override
            public void onClicked() {
                channel.invokeMethod("onClicked","android_开屏广告展示成功");
            }

            @Override
            public void onShowAdOverLay() {
                channel.invokeMethod("onShowAdOverLay","android_开屏广告展示成功");
            }

            @Override
            public void onAdCloseOverLay() {
                channel.invokeMethod("onAdCloseOverLay","android_开屏广告展示成功");
            }

            @Override
            public void onDislikeShow() {
                channel.invokeMethod("onDislikeShow","android_开屏广告展示成功");
            }

            @Override
            public void onDislikeSelected(int i, @Nullable String s) {
                channel.invokeMethod("onDislikeSelected","android_开屏广告展示成功");
            }

            @Override
            public void onDislikeCanceled() {
                channel.invokeMethod("onDislikeCanceled","android_开屏广告展示成功");
            }
        });
    }

    @Override
    public View getView() {
        return mframeLayout;
    }

    @Override
    public void dispose() {
        mframeLayout.removeAllViews();
        if(mbannerAd != null){
            mbannerAd.destroy();
        }
    }
}
